var express = require('express');
var expressWs = require("express-ws");
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();
expressWs(app);

var cors = require("cors");

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use(cors());

//测试数据的连接效果。
app.ws("/socketTest",(ws,req)=>{

    console.log("vue请求连接成功");

    ws.send("连接成功提示！！！！");

    ws.on("message",(data)=>{
        console.log("服务端发送消息了",data);
        let data1 = [
            [116.404, 39.925,{name:"张璐22清洁工",desc:"张璐喜欢偷懒，扣他200块钱今天"}],
            [116.404, 39.915,{name:"杜腾昊333",desc:"汽车信息都是他的xxxxx"}],
            [116.395, 39.935,{name:"李东泽333",desc:"东泽2201A第一号清洁工"}],
            // [116.415, 39.931],
        ]
        setInterval(()=>{
            data1.map(item=>{
                item[0] += 0.001; 
            })
            ws.send(JSON.stringify(data1));
            
        },10000);
    })
}).listen(3001);

app.use('/', indexRouter);
app.use('/users', usersRouter);

module.exports = app;
